package org.qq.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import org.qq.bean.User;
import org.qq.clientthread.ClientRegistThread;
import org.qq.util.ServerAllData;
import org.qq.util.Util;
/**
 * 
 * project_name:QQ
 * package_name:org.qq.register
 * file_name:Login.java
 * @author cui
 * create_date:2015年8月27日 下午5:39:05
 * modified_date:
 * description:用户注册窗口（登录窗口的子窗口）
 *
 */
public class Regist extends JFrame{
	private JTextField reNameField;				//帐号框
	private JTextField rePasswordField;			//密码框
	private JButton resetButton;				//撤销按钮
	private JButton ensureButton;				//注册按钮
	private JRadioButton manbutton;				//男选择扭
	private JRadioButton womenButton;			//女选择扭
	private JTextField rePasswordField1;		//确认密码框
	private JLabel reNameLabel;					//用户名
	private JLabel rePasswordLabel1;			//密码
	private JLabel ensurePasswordLabel;			//确认密码
	private ButtonGroup buttonGroup; 			//group群组
	private JTextField nickNameField;			//昵称文本框
	private JLabel nickNameLabel;				//昵称标签
	private JLabel reIpLabel;					//ip地址标签
	private JTextField ipField;					//Ip地址文本框
	private JLabel rePortLabel;					//端口标签
	private JTextField portField;				//端口文本框
	private JLabel promptNameLabel;				//账号提示标签
	private JLabel promptPasswordlabel;			//密码提示标签
	public Regist() {
		setBackground(Color.GRAY);
		getContentPane().setBackground(Color.PINK);
		this.setBounds(0, 0, 300, 485);
		this.setTitle("QQ用户注册");
		Util.screen(this);//调用Util包里面居中方法
		//this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.setResizable(false); 														// 不可改变大小
		getContentPane().setLayout(null); 												// 自定义
		this.addWindowListener(new WindowListener() {
			
			@Override
			public void windowOpened(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowIconified(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowDeiconified(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowDeactivated(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				Regist.this.setVisible(false);
				
			}
			
			@Override
			public void windowClosed(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowActivated(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		
		
		reNameField = new JTextField();
		reNameField.setFont(new Font("新宋体", Font.PLAIN, 14));
		reNameField.setBounds(96, 17, 146, 35);
		getContentPane().add(reNameField);
		reNameField.setColumns(10);
		
		rePasswordField = new JTextField(8);
		rePasswordField.setFont(new Font("新宋体", Font.PLAIN, 14));
		rePasswordField.setBounds(96, 131, 146, 35);
		getContentPane().add(rePasswordField);
		rePasswordField.setColumns(10);
		
		rePasswordField1 = new JTextField();
		rePasswordField1.setFont(new Font("新宋体", Font.PLAIN, 14));
		rePasswordField1.setBounds(96, 201, 146, 35);
		getContentPane().add(rePasswordField1);
		rePasswordField1.setColumns(10);
		
		reNameLabel = new JLabel(" 帐  号:");
		reNameLabel.setFont(new Font("新宋体", Font.PLAIN, 14));
		reNameLabel.setBounds(17, 17, 65, 35);
		getContentPane().add(reNameLabel);
		
		rePasswordLabel1 = new JLabel(" 密  码:");
		rePasswordLabel1.setFont(new Font("新宋体", Font.PLAIN, 14));
		rePasswordLabel1.setBounds(17, 131, 65, 35);
		getContentPane().add(rePasswordLabel1);
		
		ensurePasswordLabel = new JLabel(" 确认密码:");
		ensurePasswordLabel.setFont(new Font("新宋体", Font.PLAIN, 14));
		ensurePasswordLabel.setBounds(17, 201, 76, 35);
		getContentPane().add(ensurePasswordLabel);
		
		/**
		 * 确定按钮
		 */
		ensureButton = new JButton("确定");
		ensureButton.setBackground(Color.ORANGE);
		ensureButton.setFont(new Font("幼圆", Font.PLAIN, 15));
		ensureButton.setBounds(170, 394, 90, 43);
		getContentPane().add(ensureButton);
		ensureButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(!Util.isDigit(reNameField.getText())||reNameField.getText().equals("")){
					JOptionPane.showMessageDialog(Regist.this, "帐号不能为8位数字以外的其它字符!","错误提示",JOptionPane.ERROR_MESSAGE);
				}else if(!rePasswordField.getText().equals(rePasswordField1.getText())||rePasswordField1.getText().equals("")){
					JOptionPane.showMessageDialog(Regist.this, "两次密码不相同,请重新输入密码!","错误提示",JOptionPane.ERROR_MESSAGE);
				}else if(!Util.passwordIsLegal(rePasswordField1.getText())){
					JOptionPane.showMessageDialog(Regist.this,"输入密码包含非法字符,请重新输入!","错误提示",JOptionPane.ERROR_MESSAGE);
				}else if(nickNameField.getText().equals("")){
					JOptionPane.showMessageDialog(Regist.this, "昵称不能为空!","错误提示",JOptionPane.ERROR_MESSAGE);
				}else{
					User user = new User();
					user.setUserName(reNameField.getText());
					user.setNickName( nickNameField.getText());
					user.setPassword(rePasswordField1.getText());
					user.setSex( Util.sexSelect(manbutton, womenButton));//调用Util包里性别选择方法
					user.setCreatdate(new SimpleDateFormat("yyyy年MM月dd日   HH:mm:ss").format(new Date()));
					user.setIpAddress(ipField.getText());
					ServerAllData.userList.add(user);//将user信息添加到集合里
					String str1 = ServerAllData.userList.get(ServerAllData.userList.size()-1).toString();//获取user信息集合中最后索引处的值
					String strip = ipField.getText();
					String strport = portField.getText();
					Socket s =null;
					try {
						s = new Socket(strip,Integer.parseInt(strport));
						ClientRegistThread crt = new ClientRegistThread(s, Regist.this);
						crt.start();//启动用户注册线程将信息发送到服务器端
						crt.sendMessageToServer(str1);
					} catch (NumberFormatException e1) {
						e1.printStackTrace();
					} catch (UnknownHostException e1) {
						e1.printStackTrace();
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			}
		});
		
		/**
		 * 撤销(重置)按钮
		 */
		resetButton = new JButton("撤销");
		resetButton.setBackground(Color.ORANGE);
		resetButton.setFont(new Font("幼圆", Font.PLAIN, 15));
		resetButton.setBounds(44, 394, 90, 43);
		getContentPane().add(resetButton);
		resetButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				reNameField.setText("");
				nickNameField.setText("");
				rePasswordField.setText("");
				rePasswordField1.setText("");
				ipField.setText("");
				portField.setText("");
				
			}
		});
		
		buttonGroup = new ButtonGroup();
		manbutton = new JRadioButton("男",true);
		manbutton.setBackground(Color.GRAY);
		manbutton.setBounds(92, 349, 49, 23);
		buttonGroup.add(manbutton);
		getContentPane().add(manbutton);
		
		womenButton = new JRadioButton("女");
		womenButton.setBackground(Color.YELLOW);
		womenButton.setBounds(182, 349, 49, 23);
		buttonGroup.add(womenButton);
		getContentPane().add(womenButton);
		
		nickNameLabel = new JLabel(" 昵  称:");
		nickNameLabel.setFont(new Font("新宋体", Font.PLAIN, 14));
		nickNameLabel.setBounds(17, 86, 65, 35);
		getContentPane().add(nickNameLabel);
		
		nickNameField = new JTextField();
		nickNameField.setFont(new Font("新宋体", Font.PLAIN, 14));
		nickNameField.setBounds(96, 86, 146, 35);
		getContentPane().add(nickNameField);
		nickNameField.setColumns(10);
		
		reIpLabel = new JLabel(" IP 地址:");
		reIpLabel.setFont(new Font("新宋体", Font.PLAIN, 14));
		reIpLabel.setBounds(17, 246, 65, 23);
		getContentPane().add(reIpLabel);
		
		ipField = new JTextField();
		ipField.setFont(new Font("新宋体", Font.PLAIN, 14));
		ipField.setBounds(96, 246, 146, 35);
		getContentPane().add(ipField);
		ipField.setColumns(10);
		
		rePortLabel = new JLabel(" 端口号:");
		rePortLabel.setFont(new Font("新宋体", Font.PLAIN, 14));
		rePortLabel.setBounds(17, 291, 65, 35);
		getContentPane().add(rePortLabel);
		
		portField = new JTextField();
		portField.setFont(new Font("新宋体", Font.PLAIN, 14));
		portField.setBounds(96, 291, 147, 35);
		getContentPane().add(portField);
		portField.setColumns(10);
		
		promptNameLabel = new JLabel("(注: 八位阿拉伯数字组成)");
		promptNameLabel.setForeground(Color.BLACK);
		promptNameLabel.setFont(new Font("微软雅黑", Font.BOLD, 14));
		promptNameLabel.setBounds(96, 53, 165, 35);
		getContentPane().add(promptNameLabel);
		
		promptPasswordlabel = new JLabel("(注: 字母,数字,以及\"-\"以外的字符组成)");
		promptPasswordlabel.setForeground(Color.BLACK);
		promptPasswordlabel.setFont(new Font("微软雅黑", Font.BOLD, 14));
		promptPasswordlabel.setBounds(27, 168, 244, 35);
		getContentPane().add(promptPasswordlabel);

	}
}

